package com.assets;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.assets.misc.Accompaniment;
import com.assets.uhf.util.DataTransfer;
import com.senter.iot.support.openapi.uhf.UhfD2;
import com.senter.iot.support.openapi.uhf.UhfD2.UII;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdErrorCode;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdFrequencyPoint;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdOnIso18k6cRealTimeInventory;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdRssi;
import com.skyfishjy.library.RippleBackground;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2019/3/22.
 */

public class RFID_Demo extends Activity {
    private ImageView foundDevice;
    private ImageView button1;
    private Handler handler=new Handler();
    private Button stop;
    private TextView rfid_number;
    private RippleBackground rippleBackground;
    private final Accompaniment accompaniment = Accompaniment.newInstanceOfResource(RFID_Demo.this, R.raw.tag_inventoried);
    //播放声音
    private Handler accompainimentsHandler;
    private boolean state;
    private boolean stop_rfid =true;
    private List<String> data = new ArrayList<>();
    private final Runnable accompainimentRunnable = new Runnable() {
        @Override
        public void run() {
            accompaniment.start();
            accompainimentsHandler.removeCallbacks(this);
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rfid_demo);
        stop  = (Button) findViewById(R.id.rfid_stop);
        rfid_number = (TextView) findViewById(R.id.number);
        rippleBackground=(RippleBackground)findViewById(R.id.content);
        foundDevice=(ImageView)findViewById(R.id.foundDevice);
        button1 =(ImageView)findViewById(R.id.centerImage);
        HandlerThread htHandlerThread = new HandlerThread("");
        htHandlerThread.start();
        accompainimentsHandler = new Handler(htHandlerThread.getLooper());
        UhfD2.getInstance().init();
        UhfD2.getInstance().iso18k6cSetAccessEpcMatch(UhfD2.UmdEpcMatchSetting.newInstanceOfDisable());
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (stop_rfid){
                    rippleBackground.startRippleAnimation();
                    stop_rfid=false;
                    button1.setImageResource(R.drawable.phone1);
                    start_read();
                }else {
                    rippleBackground.stopRippleAnimation();
                    stop_rfid=true;
                    state =false;
                    button1.setImageResource(R.drawable.phone2);
                }

            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.size()>0){
                    Intent intent = new Intent();
                    intent.putExtra("data",data.toString());
                    RFID_Demo.this.setResult(1,intent);
                    RFID_Demo.this.finish();
                }
                else return;

            }
        });

    }
   private void start_read(){
           state=true;
           UhfD2.getInstance().iso18k6cRealTimeInventory(1, new UmdOnIso18k6cRealTimeInventory() {
               @Override
               public void onTagInventory(UII uii, UmdFrequencyPoint umdFrequencyPoint, Integer integer, UmdRssi umdRssi) {
                   if (!data.contains(DataTransfer.xGetString(uii.getBytes()).replace(" ","").substring(4))){
                       data.add(DataTransfer.xGetString(uii.getBytes()).replace(" ","").substring(4));
                       handler.postDelayed(new Runnable() {
                           @Override
                           public void run() {
                               rfid_number.setText(String.valueOf(data.size()));
                               foundDevice();
                           }
                       },500);
                       trigTagAccompainiment();//播放声音
                   }
               }

               @Override
               public void onFinishedSuccessfully(Integer integer, int i, int i1) {
                   onFinishedOnce();
               }
               private void onFinishedOnce() {
                   if (state) {
                       start_read();
                   }
               }
               @Override
               public void onFinishedWithError(UmdErrorCode umdErrorCode) {
                   onFinishedOnce();
               }
           });


   }
    private void foundDevice(){
        Random rand = new Random();
        foundDevice.setX(rand.nextInt(800 - 250 + 1) + 250);
        foundDevice.setY(rand.nextInt(1200 - 350 + 1) + 350);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(400);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        ArrayList<Animator> animatorList=new ArrayList<Animator>();
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(foundDevice, "ScaleX", 2f, 1.3f, 1f);
        animatorList.add(scaleXAnimator);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(foundDevice, "ScaleY", 2f, 1.3f, 1f);
        animatorList.add(scaleYAnimator);
        animatorSet.playTogether(animatorList);
        foundDevice.setVisibility(View.VISIBLE);
        animatorSet.start();
    }
    @Override
    protected void onDestroy() {
        if (accompainimentsHandler != null) {
            accompainimentsHandler.getLooper().quit();
        }
        accompaniment.release();
        stop_rfid=false;
        UhfD2.getInstance().uninit();
        super.onDestroy();
    }
    private void trigTagAccompainiment() {
        accompainimentsHandler.post(accompainimentRunnable);
    }


}
